//
//  ECUIVC_SearchResultList.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/25.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUIVC_SearchResultList.h"
#import "ECUIView_CourseList.h"
#import "ECUITextField_Search.h"
#import "UINavigationBar+Awesome.h"
#import "ECListArrayOfHistory.h"
@interface ECUIVC_SearchResultList ()<UITextFieldDelegate>
@property(nonatomic,strong)ECUITextField_Search *searchView;
@property(nonatomic,strong)ECUIView_CourseList *listView;

@end

@implementation ECUIVC_SearchResultList

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}
- (ECUITextField_Search *)searchView
{
    if (!_searchView) {
        WeakSelf(weakSelf);
        _searchView = [[ECUITextField_Search alloc]initWithFrame:CGRectMake(40, 5, kProportion_Width(780) + 60, 35)];
        _searchView.block_search = ^(id obj) {
            [weakSelf action_search:obj];
        };
    }
    return _searchView;
}



- (void)setSearchName:(NSString *)searchName
{
    _searchName = searchName;
    self.searchView.textField.text = _searchName;
    self.listView.keyName = _searchName;
}

- (void)addBarButtons
{
    [super addBarButtons];
//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
//    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    [button setImage:[UIImage imageNamed:@"Mine_back"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(action_dismiss) forControlEvents:UIControlEventTouchUpInside];
//    // 修改导航栏左边的item
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
}
- (void)action_dismiss
{
    [self.searchView removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)addSubviews
{
    [super addSubviews];
    WeakSelf(weakSelf);
    [self.view addSubview:self.listView];
    if (self.categorysData) {
        if (_listView.categorysData) {
            _listView.categorysData = self.categorysData;
        }
    }
}
-(ECUIView_CourseList *)listView
{
    
    if (!_listView) {
        WeakSelf(weakSelf);
        _listView = [[ECUIView_CourseList alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavHeight)];
        _listView.block_resign = ^(id obj){
            [weakSelf resignKeyBoard];
        };
    }
   
    return _listView;
}
-(void)resignKeyBoard
{
    [self.searchView endEditing:YES];
    [_listView keyNameChanged: self.searchView.textField.text];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:self.searchView];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.searchView removeFromSuperview];

}
-(void)action_search:(id)sender
{
    _listView.start = 0;
    NSString *keytitle = sender;
    _listView.keyName = keytitle;
    if (keytitle.length > 0) {
        [ECListArrayOfHistory addListArrayOfHistoryWith:keytitle];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
